package com.ybwh.springboot2.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Formatter {
    private static final ThreadLocal<DecimalFormat> DEFAULT_MONEY_FORMATTER = ThreadLocal.withInitial(() -> {
        return new DecimalFormat("#,##0.00");
    });
    private static final ThreadLocal<DecimalFormat> DEFAULT_RATE_FORMATTER = ThreadLocal.withInitial(() -> {
        return new DecimalFormat("#,##0.######");
    });
    private static final ThreadLocal<SimpleDateFormat> DEFAULT_DATE_FORMATTER = ThreadLocal.withInitial(() -> {
        return new SimpleDateFormat("yyyy-MM-dd");
    });
    private static final ThreadLocal<SimpleDateFormat> DEFAULT_DATETIME_FORMATTER = ThreadLocal.withInitial(() -> {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    });

    private Formatter() {
    }

    public static String money(Object amount) {
        return amount == null ? "0" : ((DecimalFormat)DEFAULT_MONEY_FORMATTER.get()).format(amount);
    }

    public static String date(Date date) {
        return date == null ? null : ((SimpleDateFormat)DEFAULT_DATE_FORMATTER.get()).format(date);
    }

    public static String dateTime(Date date) {
        return date == null ? null : ((SimpleDateFormat)DEFAULT_DATETIME_FORMATTER.get()).format(date);
    }

    public static String rate(Object amount) {
        return amount == null ? "0" : ((DecimalFormat)DEFAULT_RATE_FORMATTER.get()).format(amount);
    }

    public static String effTimeView(int seconds) {
        String min;
        int mod;
        if (seconds > 86400) {
            min = seconds / 86400 + "天";
            mod = seconds % 86400;
            return mod == 0 ? min : min + effTimeView(mod);
        } else if (seconds > 3600) {
            min = seconds / 3600 + "小时";
            mod = seconds % 3600;
            return mod == 0 ? min : min + effTimeView(mod);
        } else if (seconds > 60) {
            min = seconds / 60 + "分钟";
            mod = seconds % 60;
            return mod == 0 ? min : min + effTimeView(mod);
        } else {
            return seconds + "秒";
        }
    }

    public static String mapCamelCaseToUnderscore(String field) {
        StringBuilder sb = new StringBuilder();
        char[] var2 = field.toCharArray();
        int var3 = var2.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            char c = var2[var4];
            if (Character.isUpperCase(c)) {
                sb.append("_");
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }

    public static String maskPhone(String phone) {
        if (phone != null) {
            int len = phone.length();
            if (len > 10) {
                return phone.replaceFirst("(.*)\\d{4}(\\d{4})$", "$1****$2");
            }

            if (len > 6) {
                return phone.replaceFirst("^(.{3})\\d{3}(.*)", "$1***$2");
            }
        }

        return phone;
    }

    public static String toUpperCase(String field) {
        StringBuilder sb = new StringBuilder();
        char[] var2 = field.toCharArray();
        int var3 = var2.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            char c = var2[var4];
            sb.append(Character.toUpperCase(c));
        }

        return sb.toString();
    }
}